package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.rong.eventbus.EventBus;
import io.rong.eventbus.ThreadMode;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.CommonListWithSearchActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.Emergency1ListAdapter;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyAvoidExplodeAdapter;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyAvoidExplodeInnerAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.AvoidExplode1Bean;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.ProtectCollectionBean;
import patrol.mobile.com.mobilepatrol.fragment.BaseFragment;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by fuzhen.li on 2018/9/18.
 */
public class EmergencyAvoidExplode1Activity extends BaseFragment implements CallBack<DateBean> {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    @BindView(R.id.spTime)
    MySpinner spTime;
    private MySpinner mySpinner;
    private View itemTopView;
    private MySpinner spYear;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private String year ="";
    private  EmergencyAvoidExplodeAdapter emergencyAvoidExplodeAdapter;
    private  String mParam1="";
//    @Override
//    protected BaseQuickAdapter initAdapter() {
//        return new EmergencyAvoidExplodeAdapter(this);
//    }

//    @Override
//    protected String initTitleName() {
//        return ConsTitles.EMERGENCY_AVOID_EXPLOD_1;
//    }

//    @Override
//    public void configAdapter() {
//        super.configAdapter();
//
//        itemTopView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_avoid_explode_top,null);
//        spYear = itemTopView.findViewById(R.id.spYear);
//        flContent.addView(itemTopView);
//        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//               // startActivity(Emergency2Activity.class);
//            }
//        });
//
//    }

//    @Override
//    public void setListener() {
//        super.setListener();
//        spYear.getmText().setText(sdf.format(new Date()));
//        requestData(spYear.getmText().getText().toString().substring(0,4));
//        spYear.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                DatePickerUtil.getInstance(EmergencyAvoidExplode1Activity.this).setCallBack(EmergencyAvoidExplode1Activity.this);
//            }
//        });
//
//    }


    public static EmergencyAvoidExplode1Activity newInstance(String year) {
        EmergencyAvoidExplode1Activity fragment = new EmergencyAvoidExplode1Activity();
        Bundle args = new Bundle();
        args.putString("arg1", year);
        fragment.setArguments(args);
        return fragment;
    }

    public void requestData(String years) {
        HttpSender sender = new HttpSender(getContext(), baseUrl, Urls.EMERGENCY_AVOIDEXPLODE_1);
        HashMap<String, String> map = new HashMap<>();
        map.put("years", years);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                List<AvoidExplode1Bean> avoidExplode1BeanList = GsonUtil.jsonToList(listArray.toString(), AvoidExplode1Bean.class);
                emergencyAvoidExplodeAdapter.updateItems(avoidExplode1BeanList);
            }
        });
    }


//    @Override
//    public void onBack(DateBean dateBean) {
//        year = dateBean.getmYear()+"";
//        spYear.getmText().setText(dateBean.getmDate());
//        requestData(year);
//    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_avoid_explode1;
    }

    @Override
    protected void initView() {
        super.initView();

        if (getArguments() != null) {
             mParam1 = getArguments().getString("arg1");
        }
    }

    @Override
    protected void initData() {

        emergencyAvoidExplodeAdapter = new EmergencyAvoidExplodeAdapter(getContext());
        RecyclerViewHelper.initRecyclerViewV(getContext(), recyclerView,
                false, emergencyAvoidExplodeAdapter);
        recyclerView.setAdapter(emergencyAvoidExplodeAdapter);
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        spTime.getmText().setText(year+"");
        requestData(mParam1);
    }

//    public void setRequestData(String baseUrl,String  year){
//        requestData(baseUrl,year);
//    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    protected void initListener() {

        spTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerUtil.getInstance(getActivity()).setCallBack(EmergencyAvoidExplode1Activity.this);
            }
        });
        //spYear.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
        //spYear.getmText().setText(sdf.format(new Date()));
        //requestData(spYear.getmText().getText().toString().substring(0,4));
//        spYear.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                DatePickerUtil.getInstance(getContext()).setCallBack(EmergencyAvoidExplode1Activity.this);
//            }
//        });
    }


    @Override
    public void onBack(DateBean dateBean) {
        //spTime.getmText().setText(dateBean.getmYear()+"");
        //requestData(spTime.getmText().getText().toString());
    }
}
